package com.mt.baseinfo.aspect;

import com.mt.baseinfo.config.DynamicDataSourceContextHolder;
import com.mt.baseinfo.mult.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

//@Component
@Slf4j
//@Aspect
//@Order(-5)
public class DataSourceMethodAspect {


    @Before("@annotation(dataSource)")
    public void doBefore(JoinPoint point, DataSource dataSource){
        log.info("切换到数据源[{}]", dataSource.value().name());
        DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());
    }

    @After("@annotation(dataSource)")
    public void doAfter(JoinPoint point, DataSource dataSource){
        log.info("回收数据源[{}]", dataSource.value().name());
        DynamicDataSourceContextHolder.clearDataSourceType();
    }
}